// JavaScript Document

function CreateXMLHttpRequest()
{
	if (window.XMLHttpRequest )
	{
		return new XMLHttpRequest()
	}
	else if (window.ActiveXObject )
	{
		return new ActiveXObject("Microsoft.XMLHTTP")
	}
}

function PhanTrang(MaDanhMuc, page)
{
	xmlHttp = CreateXMLHttpRequest();
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
		{
			var kq = xmlHttp.responseText;
			document.getElementById("SanPhamByDanhMuc").innerHTML = kq;
		}
	}
	
	var serverURL = "components/danhmuc/detail_detail.php?MaDanhMuc="+MaDanhMuc+"&page="+page;
	serverURL += "&t" + new Date().getTime();
	
	xmlHttp.open("GET", serverURL, true);
	xmlHttp.send(null);
}

// quản lý danh mục ////////////////////////////////////////////////////////////////////////
function AddDanhMuc(MaDanhMucCha)
{
	str = '<input id="txtAdd'+MaDanhMucCha+'" name="txtAdd" type="text" value="" />&nbsp;';
	str += '<a style="cursor:pointer;font-size:small;" title="Lưu" onclick="SaveDanhMuc('+MaDanhMucCha+')"/>Lưu</a>&nbsp;';
	str += '<a style="cursor:pointer;font-size:small;" title="Hủy" onclick="CancelSave('+MaDanhMucCha+')"/>Hủy</a>';
	document.getElementById("Add"+MaDanhMucCha).innerHTML = str;
	
}

function SaveDanhMuc(MaDanhMucCha)
{
	var TenDanhMuc = document.getElementById("txtAdd"+MaDanhMucCha).value;
	if (TenDanhMuc == "")
	{
		alert("Chưa nhập tên");
		return;
	}
	xmlHttp = CreateXMLHttpRequest();

	xmlHttp.onreadystatechange = function()
	{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
			{
				var kq = xmlHttp.responseText;
				document.getElementById("ListDanhMuc").innerHTML = kq;
			}
	}
	
	var serverURL = "components/danhmuc/quanly_danhmuc.php?MaDanhMucCha="+MaDanhMucCha+"&TenDanhMuc="+TenDanhMuc+"&save=1&ajax=1";

	serverURL += "&t" + new Date().getTime();
	
	xmlHttp.open("GET", serverURL, true);
	xmlHttp.send(null);
}

function CancelSave(MaDanhMucCha)
{
	document.getElementById("Add"+MaDanhMucCha).innerHTML = '<a style="cursor:pointer;font-size:small;" title="Thêm" onclick="AddDanhMuc('+MaDanhMucCha+')"/>Thêm</a>';
	
}

function DeleteDanhMuc(MaDanhMuc)
{
	input_box = confirm('Bạn có chắc muốn xóa không?')
	if (input_box==true)
	{
		xmlHttp = CreateXMLHttpRequest();
	
		xmlHttp.onreadystatechange = function()
		{
				if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
				{
					var kq = xmlHttp.responseText;
					document.getElementById("ListDanhMuc").innerHTML = kq;
				}
		}
		
		var serverURL = "components/danhmuc/quanly_danhmuc.php?MaDanhMuc="+MaDanhMuc+"&delete=1&ajax=1";
	
		serverURL += "&t" + new Date().getTime();
		
		xmlHttp.open("GET", serverURL, true);
		xmlHttp.send(null);
	}
}

function EditDanhMuc(MaDanhMuc, TenDanhMuc)
{
	//var node = document.getElementById("Edit"+MaDanhMuc);
	var strTenDanhMuc = "'"+TenDanhMuc+"'";
	var str = '<input id="txtEdit'+MaDanhMuc+'" name="txtEdit" type="text" value="'+TenDanhMuc+'" />&nbsp;';
	str += '<a style="cursor:pointer;font-size:small;" title="Cập Nhật" onclick="UpdateDanhMuc('+MaDanhMuc+','+strTenDanhMuc+')"/>Cập Nhật</a>&nbsp;';
	str += '<a style="cursor:pointer;font-size:small;" title="Hủy" onclick="CancelUpdate('+MaDanhMuc+','+strTenDanhMuc+')"/>Hủy</a>';
	document.getElementById("Edit"+MaDanhMuc).innerHTML = str;
	document.getElementById("Tool"+MaDanhMuc).innerHTML = "";
	document.getElementById("ToolXoa"+MaDanhMuc).innerHTML = "";
	
}

function CancelUpdate(MaDanhMuc, TenDanhMuc)
{
	//var node = document.getElementById("Edit"+MaDanhMuc);
	var strTenDanhMuc = "'"+TenDanhMuc+"'";

	document.getElementById("Edit"+MaDanhMuc).innerHTML = TenDanhMuc;
	document.getElementById("Tool"+MaDanhMuc).innerHTML = '<a style="cursor:pointer;font-size:small;" title="Sửa" onclick="EditDanhMuc('+MaDanhMuc+','+strTenDanhMuc+')"/>Sửa</a>';
	document.getElementById("ToolXoa"+MaDanhMuc).innerHTML = '<a style="cursor:pointer;font-size:small;" title="Xóa" onclick="DeleteDanhMuc('+MaDanhMuc+')"/>Xóa</a>';
}

function UpdateDanhMuc(MaDanhMuc, TenDanhMuc)
{
	var TenDanhMucNew = document.getElementById("txtEdit"+MaDanhMuc).value;
	if (TenDanhMucNew == "")
	{
		alert("Chưa nhập tên");
		return;
	}
	if (TenDanhMucNew == TenDanhMuc)
	{ 
		var strTenDanhMuc = "'"+TenDanhMuc+"'";
	
		document.getElementById("Edit"+MaDanhMuc).innerHTML = TenDanhMuc;
		document.getElementById("Tool"+MaDanhMuc).innerHTML = '<a style="cursor:pointer;font-size:small;" title="Sửa" onclick="EditDanhMuc('+MaDanhMuc+','+strTenDanhMuc+')"/>Sửa</a>';
		
		return;
	}
	
	xmlHttp = CreateXMLHttpRequest();

	xmlHttp.onreadystatechange = function()
	{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
			{
				var kq = xmlHttp.responseText;
				document.getElementById("ListDanhMuc").innerHTML = kq;
			}
	}
	
	var serverURL = "components/danhmuc/quanly_danhmuc.php?MaDanhMuc="+MaDanhMuc+"&TenDanhMuc="+TenDanhMucNew+"&update=1&ajax=1";

	serverURL += "&t" + new Date().getTime();
	
	xmlHttp.open("GET", serverURL, true);
	xmlHttp.send(null);
}
